package com.example.tests;

import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Random;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;

import com.example.fw.ApplicationManager;

public class TestBase {
	
	protected ApplicationManager app;
	private int checkCounter;
	private int checkFrequency;
	
	@BeforeTest
	public void setUp() throws Exception {
		String configFile = System.getProperty("configFile", "application.properties");
		Properties properties = new Properties();
		properties.load(new FileReader(new File(configFile)));
		app = new ApplicationManager(properties);
		checkCounter = 0;
		checkFrequency = Integer.parseInt(properties.getProperty("check.frequency", "0"));
		
	}
	
	protected boolean wantToCheck() {
		checkCounter ++;
		if (checkCounter > checkFrequency) {
			checkCounter = 0;
			return true;
		} else {
			return false;
		}
	}
	
	@AfterTest
	public void tearDown() throws Exception {
		app.stop();
		
	}
	

	public String generateRandomString() {
		Random rnd = new Random();
		if (rnd.nextInt(3) == 0){
			return "";
		} else {
			return "test" + rnd.nextInt();
		}
	}
	
	public String generateRandomEmail() {
		return generateRandomString() + "@" + generateRandomString() + ".com";
	}
	public String generateRandomPhoneNumber() {
		StringBuilder phone = new StringBuilder();
		phone.append("+7 (")
		     .append(generateRandomNumberInRange(100, 999))
		     .append(") ")
		     .append(generateRandomNumberInRange(100, 999))
		     .append(" - ")
		     .append(generateRandomNumberInRange(10, 99))
		     .append(" - ")
		     .append(generateRandomNumberInRange(10, 99));
		     
		
		return phone.toString();
	}
	
	public static int generateRandomNumberInRange(int startRange, int endRange) {
		Random rnd = new Random();
		return rnd.nextInt(endRange - startRange) + startRange;
	}
			

}
